#include<bits/stdc++.h>

using namespace std;

int main(){
	long long r,y,g;scanf("%lld%lld%lld", &r,&y,&g);
	long long n;scanf("%lld", &n);
	long long time = 0;
	for (long long i = 0; i < n; ++i)
	{
		long long k,t;scanf("%lld%lld", &k,&t);
		long long tmp = time - t;
		if(k == 0){
			time += t;
		}else if(k == 1){		
			if(tmp<=0){
				time -= tmp;
			}else{
				tmp %= (r+y+g);
				if(tmp<=g){
					continue;
				}else{
					time += (r+y+g - tmp);
				}
			}
		}else if(k == 2){
			if(tmp <= 0){
				time -= tmp;
				time += r;
			}else{
				tmp %= (r+y+g);
				if(tmp<=r){	
					time += r - tmp;
				}else if(tmp<=r+g){
					continue;
				}else{
					time += r+g+y -tmp + r;
				}
			}
		}else if(k == 3){
			if(tmp >= 0){
				tmp %= (r+y+g);
				if(tmp <= y+r){
					time += y+r-tmp;
				}
			}
		}
		// printf("time = %lld\n", time);
	}
	printf("%lld\n", time);

	return 0;
}